package com.aaa.service;

import com.aaa.entity.Users;
import com.aaa.mapper.UsersMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 获取授权用户的service实现
 */
@Component
public class UsersDetailServiceImpl implements UserDetailsService {

    @Resource
    private UsersMapper usersMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper<Users> wrapper = new QueryWrapper();
        wrapper.eq("username", username);   //where username = ?
        Users users = usersMapper.selectOne(wrapper);   //
        if (users == null) {
            throw new UsernameNotFoundException("用户名输入不正确");
        }
        List<? extends GrantedAuthority> list=new ArrayList();
        List<GrantedAuthority> authorities=new ArrayList<GrantedAuthority>();
        users.setAuthorities(authorities);
        users.setDispayName(users.getUsername());
        users.setUsername(users.getUid().toString());
        return users;
    }

}


